﻿using AutoMapper;
using MediatR;
using ProduceBasicData.Api.Application.Command.Material.BOM;
using ProduceBasicData.Domain.Material;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;

namespace ProduceBasicData.Api.Application.Handler.Material.BOM
{
    /// <summary>
    /// 删除BOM
    /// </summary>
    public class DeleteBOMHandler : IRequestHandler<DeleteBOMCommand, ApiResult<int>>
    {
        private readonly IBaseRespository<BOMInfoModel> _baseRespository;

        public DeleteBOMHandler( IBaseRespository<BOMInfoModel> baseRespository)
        {
            _baseRespository = baseRespository;
        }

        public async Task<ApiResult<int>> Handle(DeleteBOMCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();

            var bom = await _baseRespository.GetModelAsync(request.Id);
            if (bom == null)
            {
                res.Code = ApiEnum.Failed;
                res.Msg = "您要删除的数据不存在";
                return res;
            }
            bom.Isdel = true;

            var r = await _baseRespository.Update(bom);
            if (r > 0)
            {
                res.Data = r;
                res.Code = ApiEnum.Success;
                res.Msg = "工序维护信息删除成功";
            }
            else
            {
                res.Code = ApiEnum.Failed;
                res.Msg = "工序维护信息删除失败";
            }
            return res;
        }
    }
}
